package com.itlong.cloud.propertyapp.service.impl;

import com.itlong.cloud.POJO.DTO.app.property.app.PropertyAppAlarmDTO;
import com.itlong.cloud.POJO.DTO.app.property.app.PropertyAppInspectDTO;
import com.itlong.cloud.POJO.PO.PropertyAlarmRecordPO;
import com.itlong.cloud.POJO.PO.PropertyInspectPointInfoPO;
import com.itlong.cloud.POJO.PO.PropertyInspectRecordPO;
import com.itlong.cloud.POJO.VO.app.property.app.PropertyAppInspectLineInfoVO;
import com.itlong.cloud.POJO.VO.app.property.app.PropertyAppInspectRecordDetailVO;
import com.itlong.cloud.POJO.VO.app.property.app.PropertyAppInspectInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.propertyapp.dao.IPropertyAppInspectDao;
import com.itlong.cloud.propertyapp.service.IPropertyAppInspectService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;

import java.util.*;

/**
 * <desc>
 *      物业APP巡检模块业务逻辑实现类
 * </desc>
 *
 * @createDate2020/1/7
 **/
@Service
public class PropertyAppInspectServiceImpl implements IPropertyAppInspectService {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyAppInspectServiceImpl.class);

    @Autowired
    private IPropertyAppInspectDao iPropertyAppInspectDao;

    /**
     * <desc>
     *      获取巡检计划列表相关统计信息
     * </desc>
     *
     * @param userId 用户id
     * @param projectId 项目id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/1/6
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyAppInspectInfoVO> getInspectPlanList(String userId, String projectId) throws Exception {
        List<PropertyAppInspectInfoVO> propertyAppInspectInfoVOS = iPropertyAppInspectDao.getInspectPlanList(userId,projectId);
        for (PropertyAppInspectInfoVO propertyAppInspectInfoVO : propertyAppInspectInfoVOS) {
            List<Map<String, Object>> inspectPointList = new ArrayList<>();
            List<String> inspectIdList = StringHandlerUtil.splitStringList(propertyAppInspectInfoVO.getInspectPointId());
            List<String> deviceUniqueList = StringHandlerUtil.splitStringList(propertyAppInspectInfoVO.getDeviceUnique());
            List<String> inspectNameList = StringHandlerUtil.splitStringList(propertyAppInspectInfoVO.getInspectPointName());
            //将巡检点id和巡检点name的map存入list
            for (int i = 0; i < inspectIdList.size(); i++) {
                Map<String, Object> inspectPointMap = new HashMap<>();
                inspectPointMap.put("inspectPointId", inspectIdList.get(i));
                inspectPointMap.put("deviceUnique",deviceUniqueList.get(i));
                inspectPointMap.put("inspectPointName", inspectNameList.get(i));
                inspectPointList.add(inspectPointMap);
            }
            propertyAppInspectInfoVO.setInspectPointList(inspectPointList);
        }
        //组装返回数据
        return propertyAppInspectInfoVOS;
    }

    /**
     * <desc>
     *      保存报警信息
     * </desc>
     *
     * @param propertyAppAlarmDTO 物业app报警参数DTO
     *
     * @return
     * @Author Zhu.yj
     * @createDate  2020/1/7
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer savePoliceCallDetail(PropertyAppAlarmDTO propertyAppAlarmDTO) throws Exception{
        PropertyAlarmRecordPO propertyAlarmRecordPO = new PropertyAlarmRecordPO();
        propertyAlarmRecordPO.setAlarmId(LogicIdUtil.bussinessId());
        propertyAlarmRecordPO.setAlarmUserId(propertyAppAlarmDTO.getUserId());
        propertyAlarmRecordPO.setInspectPlanId(propertyAppAlarmDTO.getInspectPlanId());
        propertyAlarmRecordPO.setAlarmAddress(propertyAppAlarmDTO.getCallAddress());
        propertyAlarmRecordPO.setAlarmRemark(propertyAppAlarmDTO.getContent());
        propertyAlarmRecordPO.setPosition(propertyAppAlarmDTO.getPosition());
        propertyAlarmRecordPO.setImageUrl(propertyAppAlarmDTO.getImageUrl());
        propertyAlarmRecordPO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyAlarmRecordPO.setProjectId(propertyAppAlarmDTO.getProjectId());
        propertyAlarmRecordPO.setAlarmUserPhoneNum(propertyAppAlarmDTO.getAdminPhone());
        propertyAlarmRecordPO.setCreateTime(new Date());
        iPropertyAppInspectDao.save(SqlUtil.durableData(propertyAlarmRecordPO, PlatformConstants.TABLE_SAVE));
        return 1;
    }

    /**
     * <desc>
     *      根据巡检点编号获取巡检点信息
     * </desc>
     *
     * @return 巡检点信息
     * @author wangzhi
     * @createDate 2020/01/08
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyInspectPointInfoPO getInspectPointByNum(PropertyAppInspectDTO propertyAppInspectDTO) throws Exception {
        return iPropertyAppInspectDao.getInspectPointByNum(propertyAppInspectDTO);
    }

    /**
     * <desc>
     *      保存巡检详情
     * </desc>
     *
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2020/01/08
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveInspectRecord(PropertyInspectRecordPO propertyInspectRecordPO) throws Exception {
        Date nowTime = new Date();
        propertyInspectRecordPO.setInspectId(LogicIdUtil.bussinessId());
        propertyInspectRecordPO.setCreateUserId(propertyInspectRecordPO.getUserId());
        propertyInspectRecordPO.setOperateUserId(propertyInspectRecordPO.getUserId());
        propertyInspectRecordPO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyInspectRecordPO.setCreateTime(nowTime);
        propertyInspectRecordPO.setUpdateTime(nowTime);
        return iPropertyAppInspectDao.save(SqlUtil.durableData(propertyInspectRecordPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      查询巡检记录月视图时间
     * </desc>
     *
     * @return 年月和其对应的日期列表
     * @author wangzhi
     * @createDate 2020/02/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<Map<String, Object>> getInspectRecordTime(PropertyAppInspectDTO propertyAppInspectDTO) throws Exception {
        List<Map<String, Object>> data = new ArrayList<>();
        // 数据库中查询对应巡检计划id的巡检记录的去重后的时间，年月和日期
        List<Map<String, String>> dateList = iPropertyAppInspectDao.getInspectDateByInspectPlanId(propertyAppInspectDTO);
        if (dateList != null && !dateList.isEmpty()) {
            // 处理数据
            for (Map<String, String> dateMap : dateList) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("dateString", dateMap.get("yearMonth"));
                dataMap.put("daysArray", dateMap.get("day").split(","));
                data.add(dataMap);
            }
        }
        return data;
    }

    /**
     * <desc>
     *      查询巡检记录月视图详情
     * </desc>
     *
     * @return 巡检记录月视图详情
     * @author wangzhi
     * @createDate 2020/02/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyAppInspectRecordDetailVO> getInspectRecordDetail(PropertyAppInspectDTO propertyAppInspectDTO) throws Exception {
        return iPropertyAppInspectDao.getInspectRecordDetail(propertyAppInspectDTO);
    }

    /**
     * <desc>
     *      获取巡检计划列表
     * </desc>
     *
     * @param inspectPlanId 巡检计划id
     * @param projectId 项目id
     * @param pageNum  页码
     * @return
     * @Author Zhu.yj
     * @createDate  2020/4/2
     */
    @Override
    public List<PropertyAppInspectLineInfoVO> getInspectLineList(String inspectPlanId, String projectId, String pageNum) throws Exception {
        Map<String,Object> params= new HashMap<>();
        if (Integer.parseInt(pageNum) > 1){
            params.put("startSlip",15*(Integer.parseInt(pageNum)-1));
        }else {
            params.put("startSlip",0);
        }
        params.put("pageSize",15);
        params.put("inspectPlanId",inspectPlanId);
        params.put("projectId",projectId);
        List<PropertyAppInspectLineInfoVO> propertyAppInspectLineInfoVOS = iPropertyAppInspectDao.getInspectLineList(params);
        for (PropertyAppInspectLineInfoVO propertyAppInspectLineInfoVO : propertyAppInspectLineInfoVOS){
            List<String> inspectPointNameList = StringHandlerUtil.splitStringList(propertyAppInspectLineInfoVO.getInspectPointNameList()+"");
            propertyAppInspectLineInfoVO.setInspectPointNameList(inspectPointNameList);
        }
        return propertyAppInspectLineInfoVOS;
    }

    /**
     * <desc>
     *      获取巡检点列表信息
     * </desc>
     *
     * @param inspectPlanId 巡检计划id
     * @param projectId 项目id
     * @param pageNum  页码
     * @return
     * @Author Zhu.yj
     * @createDate  2020/4/2
     */
    @Override
    public List<String> getInspectPointList(String inspectPlanId, String projectId, String pageNum) throws Exception {
        Map<String,Object> params= new HashMap<>();
        if (Integer.parseInt(pageNum) > 1){
            params.put("startSlip",15*(Integer.parseInt(pageNum)-1));
        }else {
            params.put("startSlip",0);
        }
        params.put("pageSize",15);
        params.put("inspectPlanId",inspectPlanId);
        params.put("projectId",projectId);
        List<String> inspectPointList = iPropertyAppInspectDao.getInspectPointList(params);
        return inspectPointList;
    }

}
